﻿using System.IO;
using Microsoft.Kinect;
using DetectingKinectSencor;
using System;

/// <summary>
/// The following class was provided by the book 
/// Microsoft.Press.Programming.with.the.Kinect.for.Windows.Software.Development.Kit.Sep.2012
/// </summary>
public class PlayDepthStream : FramePlayer
{
    private bool DEBUGon = MainWindow.DEBUGplayDepthStream;
    bool DEBUGstreamWriterReplayOn = MainWindow.DEBUGstreamWriterReplay;

    readonly DepthImageFrame internalFrame;
    long streamPosition;
    Stream stream;
    BinaryReader streamReader;
    public int Width { get; private set; }
    public int Height { get; private set; }
    public int BytesPerPixel { get; private set; }
    public DepthImageFormat Format { get; private set; }
    public int PixelDataLength { get; set; }


    public PlayDepthStream(DepthImageFrame frame)
    {
        Format = frame.Format;
        BytesPerPixel = frame.BytesPerPixel;
        frameNumber = frame.FrameNumber;
        timeStamp = frame.Timestamp;

        if (DEBUGon)
        {
            Console.WriteLine("PlayDepthStream Frame number: " + frameNumber);
            Console.WriteLine("PlayDepthStream Time stamp: " + timeStamp);
            Console.WriteLine("\n");
        }

        Width = frame.Width;
        Height = frame.Height;
        PixelDataLength = frame.PixelDataLength;
        internalFrame = frame;
    }
    public PlayDepthStream()
    {
    }

    internal override void CreateFromReader(BinaryReader reader)
    {
        type = "depth";
        timeStamp = reader.ReadInt64();
        BytesPerPixel = reader.ReadInt32();
        Format = (DepthImageFormat)reader.ReadInt32();
        Width = reader.ReadInt32();
        Height = reader.ReadInt32();
        frameNumber = reader.ReadInt32();
        PixelDataLength = reader.ReadInt32();
        stream = reader.BaseStream;
        streamReader = reader;
        streamPosition = stream.Position;
        stream.Position += PixelDataLength * 2;
    }
    public void CopyPixelDataTo(short[] pixelData)
    {

        // If the pictures are from the kinect than is this if entered, if read from previous saved 
        // stream than the code below is used
        if (internalFrame != null)
        {
            internalFrame.CopyPixelDataTo(pixelData);
            return;
        }

        long savedPosition = stream.Position;
        stream.Position = streamPosition;

        byte[] depthBytesTemp = new byte[2];

        for (int index = 0; index < PixelDataLength; index++)
        {

            depthBytesTemp[0] = streamReader.ReadByte();
            depthBytesTemp[1] = streamReader.ReadByte();

            pixelData[index] = BitConverter.ToInt16(depthBytesTemp,0);
        }

        stream.Position = savedPosition;
    }

    public static implicit operator PlayDepthStream(DepthImageFrame frame)
    {
        return new PlayDepthStream(frame);
    }
}